﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Database;
using DSTDNVR;
using ReflectionStudio.Core.FTPClient;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for DSTDNVR.xaml
    /// </summary>
    public partial class DSTDNVR : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        DataTable tableUI;
        private DataRow dwUI;
        DSTDNVRData m_Data, m_DataX;
        public SETVV setUI
        {
            get
            {
                return (SETVV)this.TryFindResource("SETUI");
            }
        }
        public DataRow dwVKPOS;
        public DataRow dwVVLIEFSCH;
        public DataRow dwCOLOR;
        public DataRow dwWBBST;
        public DataSet dsParamaters;
        DSTDNVRReport m_rpt;
        ArrayList listVV06, ListColor, listBatchNo;
        private string fromshippingdate = "", endshippingdate = "";
        #endregion
        public DSTDNVR()
        {
            InitializeComponent();
            this.Name = "销售开票情况";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            m_Data = new DSTDNVRData(dicParamater);
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("SO", typeof(string));
            tableUI.Columns.Add("MAN", typeof(string));
            tableUI.Columns.Add("CUSTOMER", typeof(string));
            tableUI.Columns.Add("EXP", typeof(string));
            tableUI.Columns.Add("USER", typeof(string));
            tableUI.Columns.Add("FROMDATE", typeof(string));
            tableUI.Columns.Add("ENDDATE", typeof(string));
            dwUI = tableUI.NewRow();
            SetUpFtp();
            btnLogFTP_Click(null, null);
            /*
            txtVKPAUFKRS.Text = "Z ";
            txtVKPAUFLFD.Text = "002030";
             * */

        }
        public void btnLogFTP_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string ftpsite = txtFTPSite.Text.Trim();
                string ftpport = txtFTPPort.Text.Trim();
                string ftpuser = txtFTPUser.Text.Trim();
                CheckFtp(ftpsite, ftpport, ftpuser, txtFTPPwd, IsBelongs2Admin.IsChecked == true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show("FTP登录信息有误。请联系你们的破管理员。");
            }
            finally
            {
                ftpClient.Close();
            }
        }
  
        private void btnQuery_Click(object sender, EventArgs e)
        {
            string PrintType = "JustExcel";
            GenerateReport(PrintType);
        }
        private void GenerateReport(string PrintType)
        {
            try
            {
                m_rpt = new DSTDNVRReport(dicParamater);
                m_rpt.dsSource = setUI;
                m_rpt.PrintType = PrintType;
                m_rpt.ftpClient = ftpClient;
                if (m_Data.exAgent._app == null)
                {
                    m_Data.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
                }
                m_rpt.proxy = this.proxy;
                m_rpt.OracleDB = this.OracleDB;
                m_rpt.exAgent = m_Data.exAgent;
                m_rpt.WriteExcelReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "DirectPrint";
            GenerateReport(PrintType);
        }

        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }

        private void FROM_SHIPPING_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromshippingdate = "";
                fromshippingdate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void END_SHIPPING_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                endshippingdate = "";
                endshippingdate = m_Data.GetDate(sender);
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnGetDNVR_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                setUI.Clear();
                setUI.AcceptChanges();
                dwUI.Delete();
                string SO = txtSO.Text.ToString().Trim();
                string MAN = txtMAN.Text.ToString().Trim();
                string CUSTOMER = m_Data.Right("00000000" +txtCUSTOMER.Text.ToString().Trim(),8);
                string EXP = txtEXP.Text.ToString().Trim();
                string USER = txtUSER.Text.ToString().Trim();

                if (SO == "")
                {
                    SO = "AAA";
                } 
                if (MAN == "")
                {
                    MAN = "AAA";
                }
                if (EXP == "")
                {
                    EXP = "AAA";
                }
                if (USER == "")
                {
                    USER = "AAA";
                }
                if (fromshippingdate == "")
                {
                    fromshippingdate = "20000000";
                }
                if (endshippingdate == "")
                {
                    endshippingdate = "99999999";
                }
                dwUI["SO"] = SO;
                dwUI["MAN"] = MAN;
                dwUI["CUSTOMER"] = CUSTOMER;
                dwUI["EXP"] = EXP; 
                dwUI["USER"] = USER;
                dwUI["FROMDATE"] = fromshippingdate;
                dwUI["ENDDATE"] = endshippingdate;
                dsResult = m_Data.BuildData(dwUI);
                setUI.Merge(dsResult);
                dlgDNVR.DataContext = setUI.Tables["DNVR"];

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
